package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.qiniu.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    //注入DAO对象
    @Autowired
    private CheckItemDao checkItemDao;
    public void add(CheckItem checkItem) {
        //code name校验，1.添加唯一索引 2.查询code = #{code} or name = #{name}
        checkItemDao.add(checkItem);
    }

    //检查项分页查询

    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成

        //select * from t_checkitem limit 0,10
//        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
//        List<CheckItem> rows = page.getResult();
//        return new PageResult(page.getTotal(),rows);

          //PageHelper.startPage(currentPage,pageSize);
//        List<CheckItem> rows = checkItemDao.selectByConditionAll(queryString);
//        PageInfo page = new PageInfo(rows);
//        long total = page.getTotal();
//        return new PageResult(total,rows);

        //select count(0) from where
        //select * from where  limit
        //null "" 这两种情况不需要加百分号
        if(!StringUtils.isNullOrEmpty(queryString)){
            queryString = "%" + queryString + "%"; //%null%
        }
        //1.正常分页
        final String param = queryString;
        PageHelper.startPage(currentPage, pageSize);
        List<CheckItem> list = checkItemDao.selectByConditionAll(queryString);
        PageInfo<CheckItem> checkItemPageInfo = new PageInfo<>(list);
        long total = checkItemPageInfo.getTotal();
        //2.查询所有数据
        List<CheckItem> checkItems = checkItemDao.selectByConditionAll(null);
        //3.正常分页写法优化
        PageInfo pageInfo = PageHelper.startPage(currentPage, pageSize)
                .doSelectPageInfo(() -> checkItemDao.selectByConditionAll(param));

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    public List<CheckItem> findAll(){
        return checkItemDao.selectByConditionAll(null);
    }
    //根据ID删除检查项
    public int deleteById(Integer id) {
        //判断当前检查项是否已经关联到检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count > 0){
            //当前检查项已经被关联到检查组，不允许删除
            return CheckItemService.DELETE_BYID_ERROR_CONTAINS;
        }
        checkItemDao.deleteById(id);
        return CheckItemService.DELETE_BYID_SUCCESS;

    }

    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }
}
